var Musiker = {
	frames			: {}, 			// Container frames object
	currentModule	: null,
	Modules			: Array(),
	demoMode		: false,
	Sidebar			: {}, 
	
	PLAYICONHTML	: "<img src='images/playIcon.png'>",
	PAUSEICONHTML	: "<img src='images/pauseIcon.png'>"
}

Musiker.RegisterModule = function (module) {
	Musiker.Modules.append(module);
}

Musiker.Initialize = function () {
	LoadProgress.completeModule('LoadFinished');
	
	//create player frame
	var aFrame = document.createElement('div');
		aFrame.className = 'cssHeaderPanel';
		Musiker.frames.HeaderPanel = aFrame
	document.getElementsByTagName('body')[0].appendChild(aFrame);
	
	//create sidebar frame
	var aFrame = document.createElement('div');
		aFrame.className = 'cssSidebarPanel';
		Musiker.frames.SidebarPanel = aFrame
	document.getElementsByTagName('body')[0].appendChild(aFrame);
	
	//create main content frame
	var aFrame = document.createElement('div');
		aFrame.className = 'cssDisplayPanel';
		Musiker.frames.DisplayPanel = aFrame
	document.getElementsByTagName('body')[0].appendChild(aFrame);
	
	//------------------------------------------------------------------------------------------------------
	//populate sidebar
	
	var aFrame = document.createElement('div');
		aFrame.className = 'cssSidebar_HeaderRow';
		aFrame.innerHTML = "LIBRARY";
	Musiker.frames.SidebarPanel.appendChild(aFrame);
		
	//sidebar action bar
	var aFrame = document.createElement('div');
		aFrame.className = 'cssSidebarActionBar';
		aFrame.innerHTML = "<img src='images/sidebarActionBarButton_disabled.gif'><a href='index.php?logout'>Signoff</a>";
		Musiker.Sidebar.actionBarPanel = aFrame;
	Musiker.frames.SidebarPanel.appendChild(aFrame);
	
	
	//---------------------------------------------------------------------------------------------------------
	//build all modules
	for (i=0;i<Musiker.Modules.length;i++) {
		var m=Musiker.Modules[i];
		if (m.Initialize) m.Initialize();
	}
	
	//---------------------------------------------------------------------------------------------------------
	//start all modules

	for (i=0;i<Musiker.Modules.length;i++) {
		var m=Musiker.Modules[i];
		if (m.Start) m.Start();
	}
	
	
}

Musiker.selectModule = function (module) {
	if (module !== Musiker.currentModule) {
		if (Musiker.currentModule) Musiker.currentModule.hide();
		Musiker.currentModule = module;
		module.show();
	}
}


///	SIDEBAR -----------------------------------------------------------------------------
Musiker.Sidebar = {
	currentSelection 	: 	null
}
Musiker.Sidebar.setSelection = function (element) {
	if (Musiker.Sidebar.currentSelection) Musiker.Sidebar.currentSelection.removeAttribute("activeSelect");
	Musiker.Sidebar.currentSelection = element;
	element.setAttribute("activeSelect","yes");	
}


Musiker.buildRow = function (o, idx, oddfield) {
	var aFrame = document.createElement('tr');
		aFrame.className = 'cssPlaylistItem';
		if (oddfield) aFrame.setAttribute('oddrow',"yes");
		aFrame.onmousedown = function () {return false;}
		aFrame.onclick = function () {
			if (this.parentNode.currentSelect) this.parentNode.currentSelect.removeAttribute('selectedRow');
			this.parentNode.currentSelect = this
			this.setAttribute('selectedRow','yes');
		}

		var bFrame = document.createElement('td');
			if (idx) bFrame.innerHTML = idx.toString();
			bFrame.className = 'cssPlaylistItem_Num';
		aFrame.appendChild(bFrame);
		var bFrame = document.createElement('td');
			bFrame.innerHTML = '<div>'+o.title+'</div>';
			bFrame.className = 'cssPlaylistItem_Title';
		aFrame.appendChild(bFrame);
		var bFrame = document.createElement('td');
			if (o.artist) bFrame.innerHTML = '<div>'+o.artist+'</div>';
			bFrame.className = 'cssPlaylistItem_Artist';
		aFrame.appendChild(bFrame);
		var bFrame = document.createElement('td');
			if (o.album) bFrame.innerHTML = '<div>'+o.album+'</div>';
			bFrame.className = 'cssPlaylistItem_Album';
		aFrame.appendChild(bFrame);
		var bFrame = document.createElement('td');
			if (o.time) bFrame.innerHTML = '<div>'+ConvertSeconds(o.time)+'</div>';
			bFrame.className = 'cssPlaylistItem_Time';
		aFrame.appendChild(bFrame);
		var bFrame = document.createElement('td');
			if (o.rating) bFrame.innerHTML = '<div>'+MUSIKER_STARS[Math.floor(o.rating / 20)]+'</div>';
			bFrame.className = 'cssPlaylistItem_Rating';
		aFrame.appendChild(bFrame);


		var bFrame = document.createElement('td'); //filler cell
		aFrame.appendChild(bFrame);

	return aFrame;
}


///	KEYBOARD EVENTS---------------------------------------------------------------
document.onkeydown = function (e) {
	if (!e && window.event) e = window.event;
	var pressedKey = e.keyCode;
	
	switch (pressedKey) {
		case 32: //spacebar
			MediaPlayer.togglePlayback();
			break
			
		case 39: //right
			MediaPlayer.nextTrack();
			break
			
		case 37: //left
			MediaPlayer.back();
			break
			
		case 38: //up
			MediaPlayer.setVolume(Math.min(MediaPlayer.currentVolume+10,100));
			break
			
		case 40: //down
			MediaPlayer.setVolume(Math.max(MediaPlayer.currentVolume-10,0));
			break
			
		default: return true;
	}
	return false;
}

MUSIKER_SHOWPANEL = function () {
	this.style.visibility = "inherit";
}

MUSIKER_HIDEPANEL = function () {
	this.style.visibility = "hidden";
}

//debug function, can be removed later
function PrintObject (o) {
	var s = '[';
	for (var key in o)
		s = s + key + " => " + o[key] + '<br>'+"\n";
	s += ']';
	return s;
}

LoadProgress.completeModule('MusikerMain');